﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using D=Bids.Core.DomainObjects;
using Bids.Data;

namespace Bids.Service
{
    public interface ISystemService
    {
        void InitConfig();
        D.System Sys{get;}
    }
    public class SystemService : ISystemService
    {
        IRepository<D.System> _iRpoSys;
        public SystemService(IRepository<D.System> iRpoSys)
        {
            _iRpoSys = iRpoSys;
        }
        public void InitConfig()
        {
            HttpContext.Current.Application["System"] = _iRpoSys.FindAll().OrderBy(t=>t.ID).FirstOrDefault();
        }
        public D.System Sys 
        {
            get
            {
                return GetConfig();
            }
        }
        private D.System GetConfig()
        {
            if (HttpContext.Current.Application["System"] != null)
                return (D.System)HttpContext.Current.Application["System"];
            else
            {
                var config = _iRpoSys.FindAll().OrderBy(t => t.ID).FirstOrDefault();
                HttpContext.Current.Application["System"] = config;
                return config;
            }
        }
    }
}
